
package com.jf.cloud.rbac.controller;

import cn.hutool.core.util.BooleanUtil;
import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.rbac.dto.MenuPermissionDTO;
import com.jf.cloud.rbac.model.MenuPermission;
import com.jf.cloud.rbac.service.MenuPermissionService;
import com.jf.cloud.rbac.vo.MenuPermissionVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/09/02
 */
@RequestMapping(value = "/mp/menu_permission")
@RestController
@Tag(name = "权限接口")
public class MenuPermissionController {

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @Autowired
    private MenuPermissionService menuPermissionService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/list_by_menu")
    @Operation(summary = "获取菜单资源列表" , description = "分页获取菜单资源列表")
    public ServerResponseEntity<List<MenuPermissionVO>> listByMenuId(Long menuId) {
        List<MenuPermissionVO> menuPermissionVOList = menuPermissionService.listByMenuId(menuId);
        return ServerResponseEntity.success(menuPermissionVOList);
    }

    @GetMapping
    @Operation(summary = "获取菜单资源" , description = "根据menuPermissionId获取菜单资源")
    public ServerResponseEntity<MenuPermissionVO> getByMenuPermissionId(@RequestParam Long menuPermissionId) {
        return ServerResponseEntity.success(menuPermissionService.getByMenuPermissionId(menuPermissionId));
    }

    @PostMapping
    @Operation(summary = "保存菜单资源" , description = "保存菜单资源")
    public ServerResponseEntity<Void> save(@Valid @RequestBody MenuPermissionDTO menuPermissionDTO) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限修改菜单");
        }
        MenuPermission menuPermission = mapperFacade.map(menuPermissionDTO, MenuPermission.class);
        menuPermission.setMenuPermissionId(null);
        return menuPermissionService.save(menuPermission);
    }

    @PutMapping
    @Operation(summary = "更新菜单资源" , description = "更新菜单资源")
    public ServerResponseEntity<Void> update(@Valid @RequestBody MenuPermissionDTO menuPermissionDTO) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限修改菜单");
        }
        MenuPermission menuPermission = mapperFacade.map(menuPermissionDTO, MenuPermission.class);
        return menuPermissionService.update(menuPermission);
    }

    @DeleteMapping
    @Operation(summary = "删除菜单资源" , description = "根据菜单资源id删除菜单资源")
    public ServerResponseEntity<Void> delete(@RequestParam Long menuPermissionId) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限修改菜单");
        }
        MenuPermissionVO menuPermissionVO = menuPermissionService.getByMenuPermissionId(menuPermissionId);
        if (Objects.isNull(menuPermissionVO)) {
            throw new LuckException("找不到对应资源");
        }
        menuPermissionService.deleteById(menuPermissionId, menuPermissionVO.getBizType());
        return ServerResponseEntity.success();
    }

    @GetMapping(value = "/list")
    @Operation(summary = "获取当前用户拥有的权限" , description = "当前用户所拥有的所有权限，精确到按钮，实际上element admin里面的roles就可以理解成权限")
    public ServerResponseEntity<List<String>> permissions() {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        return ServerResponseEntity.success(menuPermissionService.listUserPermissions(userInfoInTokenBO.getUserId(),
                userInfoInTokenBO.getSysType(), com.jf.cloud.common.util.BooleanUtil.isTrue(userInfoInTokenBO.getIsAdmin())));
    }

    @GetMapping(value = "/page")
    @Operation(summary = "获取当前用户拥有的权限" , description = "当前用户所拥有的所有权限，精确到按钮，实际上element admin里面的roles就可以理解成权限")
    public ServerResponseEntity<PageVO<MenuPermissionVO>> page(PageDTO pageDTO, @ParameterObject MenuPermissionDTO menuPermissionDTO) {
        PageVO<MenuPermissionVO> permissionPage = menuPermissionService.page(pageDTO, menuPermissionDTO);
        return ServerResponseEntity.success(permissionPage);
    }
}
